/*
 * ClientView.java
 */

package client;

import Chat.IpChat_Main;
import java.awt.Color;
import java.awt.Cursor;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.soap.SOAPBinding.Style;
import javax.print.attribute.HashAttributeSet;
import javax.swing.AbstractAction;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import typinggame.*;


/**
 * The application's main frame.
 */
public class ClientView extends FrameView {
    public static ServerSocket socket;
    public static ServerComm serverComm;
    private static String ip = "172.17.180.145";
    private static String status = "Not connected";
    private static String _myUser = "";
    private static ArrayList<Hashtable<String, String>> user_list = new ArrayList<Hashtable<String, String>>();
    private static ArrayList<Hashtable<String, String>> game_list = new ArrayList<Hashtable<String, String>>();
    private static ArrayList<Hashtable> game_users = new ArrayList<Hashtable>();
    private static IpChat_Main chat = null;
    private static Timer update_player_timer = null;
    private static Timer update_game_timer = null;
    private static int _max_user = 0;
    private static boolean is_game_owner = false;
    private static String game_owner = null;
    private static gameplayview thisview = null;
    private static int _myTeam = 0;
    GameInfo gameinfo;


    public ClientView(SingleFrameApplication app) {
        
        super(app);
        serverComm = new ServerComm();
  //     chat = new IpChat_Main(this);
        initComponents();
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        
        });
        JFrame mainFrame = ClientApp.getApplication().getMainFrame();
    //    mainFrame.setSize(1300, 800);
        diolog_login.setLocationRelativeTo(mainFrame);
        diolog_login.setVisible(true);
        team_select_panel.setVisible(false);
        player_game_list_panel.setVisible(true);
        game.setVisible(false);
        btn_start.setVisible(false);
    }
    
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = ClientApp.getApplication().getMainFrame();
            aboutBox = new ClientAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        ClientApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        game = new typinggame.TypingGame();
        player_game_list_panel = new javax.swing.JPanel();
        UI_online_player = new java.awt.List();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        UI_online_games = new java.awt.List();
        txt_on_player = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        btn_create = new javax.swing.JButton();
        btn_join = new javax.swing.JButton();
        team_select_panel = new javax.swing.JPanel();
        UI_list_team_1 = new java.awt.List();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        UI_list_team_2 = new java.awt.List();
        btn_leave = new javax.swing.JButton();
        btn_start = new javax.swing.JButton();
        btn_to_team2 = new javax.swing.JButton();
        btn_to_team_1 = new javax.swing.JButton();
        chatPanel = new javax.swing.JPanel();
        chatM = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        chatH = new javax.swing.JTextPane();
        sendT = new javax.swing.JButton();
        sendA = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        settingsMenu = new javax.swing.JMenu();
        serverProperties = new javax.swing.JMenuItem();
        Account = new javax.swing.JMenu();
        meunItemLogin = new javax.swing.JMenuItem();
        menuItemLogout = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        diolog_register = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txt_new_user = new javax.swing.JTextField();
        pwd_password = new javax.swing.JPasswordField();
        pwd_password2 = new javax.swing.JPasswordField();
        btn_reg_send = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txt_server_rgt = new javax.swing.JTextField();
        diolog_login = new javax.swing.JDialog();
        panel_sign_in = new javax.swing.JPanel();
        label_password = new javax.swing.JLabel();
        label_user = new javax.swing.JLabel();
        TBox_user_name = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        button_login = new javax.swing.JButton();
        btn_register = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txt_server_ip = new javax.swing.JTextField();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setMaximumSize(new java.awt.Dimension(100, 100));
        mainPanel.setMinimumSize(new java.awt.Dimension(100, 100));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(100, 100));

        jLayeredPane1.setMinimumSize(new java.awt.Dimension(1000, 503));
        jLayeredPane1.setName("jLayeredPane1"); // NOI18N

        game.setName("game"); // NOI18N

        javax.swing.GroupLayout gameLayout = new javax.swing.GroupLayout(game);
        game.setLayout(gameLayout);
        gameLayout.setHorizontalGroup(
            gameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1500, Short.MAX_VALUE)
        );
        gameLayout.setVerticalGroup(
            gameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
        );

        game.setBounds(0, 0, 1500, 800);
        jLayeredPane1.add(game, javax.swing.JLayeredPane.DEFAULT_LAYER);

        player_game_list_panel.setName("player_game_list_panel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(client.ClientApp.class).getContext().getResourceMap(ClientView.class);
        UI_online_player.setBackground(resourceMap.getColor("UI_online_player.background")); // NOI18N
        UI_online_player.setName("UI_online_player"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        UI_online_games.setBackground(resourceMap.getColor("UI_online_games.background")); // NOI18N
        UI_online_games.setName("UI_online_games"); // NOI18N

        txt_on_player.setText(resourceMap.getString("txt_on_player.text")); // NOI18N
        txt_on_player.setName("txt_on_player"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        btn_create.setText(resourceMap.getString("btn_create.text")); // NOI18N
        btn_create.setName("btn_create"); // NOI18N
        btn_create.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_createMouseClicked(evt);
            }
        });

        btn_join.setText(resourceMap.getString("btn_join.text")); // NOI18N
        btn_join.setName("btn_join"); // NOI18N
        btn_join.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_joinMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout player_game_list_panelLayout = new javax.swing.GroupLayout(player_game_list_panel);
        player_game_list_panel.setLayout(player_game_list_panelLayout);
        player_game_list_panelLayout.setHorizontalGroup(
            player_game_list_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(player_game_list_panelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(player_game_list_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btn_create)
                    .addGroup(player_game_list_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(UI_online_player, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)
                        .addGroup(player_game_list_panelLayout.createSequentialGroup()
                            .addComponent(jLabel8)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(txt_on_player, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(player_game_list_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addGroup(player_game_list_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btn_join)
                        .addComponent(UI_online_games, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        player_game_list_panelLayout.setVerticalGroup(
            player_game_list_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(player_game_list_panelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(player_game_list_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(player_game_list_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(UI_online_player, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(UI_online_games, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(player_game_list_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(player_game_list_panelLayout.createSequentialGroup()
                        .addGroup(player_game_list_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(txt_on_player, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_create))
                    .addComponent(btn_join))
                .addContainerGap())
        );

        player_game_list_panel.setBounds(10, 10, 392, 397);
        jLayeredPane1.add(player_game_list_panel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        team_select_panel.setName("team_select_panel"); // NOI18N

        UI_list_team_1.setBackground(resourceMap.getColor("UI_list_team_1.background")); // NOI18N
        UI_list_team_1.setName("UI_list_team_1"); // NOI18N

        jLabel10.setFont(resourceMap.getFont("jLabel10.font")); // NOI18N
        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jLabel11.setFont(resourceMap.getFont("jLabel11.font")); // NOI18N
        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        UI_list_team_2.setBackground(resourceMap.getColor("UI_list_team_2.background")); // NOI18N
        UI_list_team_2.setName("UI_list_team_2"); // NOI18N

        btn_leave.setText(resourceMap.getString("btn_leave.text")); // NOI18N
        btn_leave.setName("btn_leave"); // NOI18N
        btn_leave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_leaveMouseClicked(evt);
            }
        });

        btn_start.setText(resourceMap.getString("btn_start.text")); // NOI18N
        btn_start.setName("btn_start"); // NOI18N
        btn_start.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_startMouseClicked(evt);
            }
        });

        btn_to_team2.setText(resourceMap.getString("btn_to_team2.text")); // NOI18N
        btn_to_team2.setName("btn_to_team2"); // NOI18N
        btn_to_team2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_to_team2MouseClicked(evt);
            }
        });

        btn_to_team_1.setText(resourceMap.getString("btn_to_team_1.text")); // NOI18N
        btn_to_team_1.setName("btn_to_team_1"); // NOI18N
        btn_to_team_1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_to_team_1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout team_select_panelLayout = new javax.swing.GroupLayout(team_select_panel);
        team_select_panel.setLayout(team_select_panelLayout);
        team_select_panelLayout.setHorizontalGroup(
            team_select_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(team_select_panelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(team_select_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(UI_list_team_1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(team_select_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn_to_team2)
                    .addComponent(btn_to_team_1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(team_select_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addGroup(team_select_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(team_select_panelLayout.createSequentialGroup()
                            .addComponent(btn_start)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btn_leave))
                        .addComponent(UI_list_team_2, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(22, 22, 22))
        );
        team_select_panelLayout.setVerticalGroup(
            team_select_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(team_select_panelLayout.createSequentialGroup()
                .addGroup(team_select_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(team_select_panelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(team_select_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(team_select_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(UI_list_team_1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(UI_list_team_2, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(team_select_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btn_leave)
                            .addComponent(btn_start)))
                    .addGroup(team_select_panelLayout.createSequentialGroup()
                        .addGap(115, 115, 115)
                        .addComponent(btn_to_team2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_to_team_1)))
                .addContainerGap(48, Short.MAX_VALUE))
        );

        team_select_panel.setBounds(10, 10, 453, 403);
        jLayeredPane1.add(team_select_panel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        chatPanel.setName("chatPanel"); // NOI18N

        chatM.setColumns(20);
        chatM.setRows(5);
        chatM.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        chatM.setName("chatM"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        chatH.setEditable(false);
        chatH.setName("chatH"); // NOI18N
        jScrollPane1.setViewportView(chatH);

        sendT.setText(resourceMap.getString("sendT.text")); // NOI18N
        sendT.setName("sendT"); // NOI18N
        sendT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                sendTMouseClicked(evt);
            }
        });

        sendA.setText(resourceMap.getString("sendA.text")); // NOI18N
        sendA.setName("sendA"); // NOI18N
        sendA.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                sendAMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout chatPanelLayout = new javax.swing.GroupLayout(chatPanel);
        chatPanel.setLayout(chatPanelLayout);
        chatPanelLayout.setHorizontalGroup(
            chatPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chatPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(chatPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, chatPanelLayout.createSequentialGroup()
                        .addComponent(chatM, javax.swing.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sendT)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sendA))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 839, Short.MAX_VALUE))
                .addContainerGap())
        );
        chatPanelLayout.setVerticalGroup(
            chatPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chatPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(chatPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(chatPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(sendA, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(sendT, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(chatM, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1000, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chatPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 503, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                .addComponent(chatPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(client.ClientApp.class).getContext().getActionMap(ClientView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        settingsMenu.setText(resourceMap.getString("settingsMenu.text")); // NOI18N
        settingsMenu.setName("settingsMenu"); // NOI18N

        serverProperties.setLabel(resourceMap.getString("serverProperties.label")); // NOI18N
        serverProperties.setName("serverProperties"); // NOI18N
        settingsMenu.add(serverProperties);

        menuBar.add(settingsMenu);

        Account.setLabel(resourceMap.getString("Account.label")); // NOI18N
        Account.setName("Account"); // NOI18N

        meunItemLogin.setLabel("Sign in");
        meunItemLogin.setText("Sign in");
        meunItemLogin.setName("meunItemLogin"); // NOI18N
        meunItemLogin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                meunItemLoginMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                meunItemLoginMousePressed(evt);
            }
        });
        Account.add(meunItemLogin);

        menuItemLogout.setLabel(resourceMap.getString("menuItemLogout.label")); // NOI18N
        menuItemLogout.setLabel("Sign out");
        menuItemLogout.setName("menuItemLogout"); // NOI18N
        menuItemLogout.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                menuItemLogoutMouseClicked(evt);
            }
        });
        Account.add(menuItemLogout);

        Account.setText("Account");

        menuBar.add(Account);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setText(resourceMap.getString("statusMessageLabel.text")); // NOI18N
        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setText(resourceMap.getString("statusAnimationLabel.text")); // NOI18N
        statusAnimationLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        statusAnimationLabel.setDebugGraphicsOptions(javax.swing.DebugGraphics.FLASH_OPTION);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 1058, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 951, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        diolog_register.setTitle(resourceMap.getString("diolog_register.title")); // NOI18N
        diolog_register.setMinimumSize(new java.awt.Dimension(300, 220));
        diolog_register.setName("diolog_register"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txt_new_user.setText(resourceMap.getString("txt_new_user.text")); // NOI18N
        txt_new_user.setName("txt_new_user"); // NOI18N

        pwd_password.setText(resourceMap.getString("pwd_password.text")); // NOI18N
        pwd_password.setName("pwd_password"); // NOI18N

        pwd_password2.setText(resourceMap.getString("pwd_password2.text")); // NOI18N
        pwd_password2.setName("pwd_password2"); // NOI18N

        btn_reg_send.setText(resourceMap.getString("btn_reg_send.text")); // NOI18N
        btn_reg_send.setName("btn_reg_send"); // NOI18N
        btn_reg_send.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_reg_sendMouseClicked(evt);
            }
        });

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        txt_server_rgt.setText(resourceMap.getString("txt_server_rgt.text")); // NOI18N
        txt_server_rgt.setName("txt_server_rgt"); // NOI18N

        javax.swing.GroupLayout diolog_registerLayout = new javax.swing.GroupLayout(diolog_register.getContentPane());
        diolog_register.getContentPane().setLayout(diolog_registerLayout);
        diolog_registerLayout.setHorizontalGroup(
            diolog_registerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diolog_registerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(diolog_registerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(diolog_registerLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(pwd_password2, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
                    .addComponent(btn_reg_send, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(diolog_registerLayout.createSequentialGroup()
                        .addGroup(diolog_registerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5))
                        .addGap(62, 62, 62)
                        .addGroup(diolog_registerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt_new_user, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                            .addComponent(pwd_password, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                            .addComponent(txt_server_rgt, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE))))
                .addContainerGap())
        );
        diolog_registerLayout.setVerticalGroup(
            diolog_registerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, diolog_registerLayout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addGroup(diolog_registerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(txt_server_rgt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(diolog_registerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txt_new_user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(diolog_registerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(pwd_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(diolog_registerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(pwd_password2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btn_reg_send)
                .addContainerGap())
        );

        diolog_login.setTitle(resourceMap.getString("diolog_login.title")); // NOI18N
        diolog_login.setAlwaysOnTop(true);
        diolog_login.setMinimumSize(new java.awt.Dimension(260, 180));
        diolog_login.setName("diolog_login"); // NOI18N
        diolog_login.setResizable(false);

        panel_sign_in.setName("panel_sign_in"); // NOI18N

        label_password.setText(resourceMap.getString("label_password.text")); // NOI18N
        label_password.setName("label_password"); // NOI18N

        label_user.setText(resourceMap.getString("label_user.text")); // NOI18N
        label_user.setName("label_user"); // NOI18N

        TBox_user_name.setText(resourceMap.getString("TBox_user_name.text")); // NOI18N
        TBox_user_name.setMinimumSize(new java.awt.Dimension(22, 22));
        TBox_user_name.setName("TBox_user_name"); // NOI18N

        jPasswordField1.setText(resourceMap.getString("jPasswordField1.text")); // NOI18N
        jPasswordField1.setName("jPasswordField1"); // NOI18N

        button_login.setText(resourceMap.getString("button_login.text")); // NOI18N
        button_login.setName("button_login"); // NOI18N
        button_login.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button_loginMouseClicked(evt);
            }
        });

        btn_register.setAction(actionMap.get("act_register")); // NOI18N
        btn_register.setLabel(resourceMap.getString("btn_register.label")); // NOI18N
        btn_register.setName("btn_register"); // NOI18N
        btn_register.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_registerMouseClicked(evt);
            }
        });

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        txt_server_ip.setText(resourceMap.getString("txt_server_ip.text")); // NOI18N
        txt_server_ip.setName("txt_server_ip"); // NOI18N

        javax.swing.GroupLayout panel_sign_inLayout = new javax.swing.GroupLayout(panel_sign_in);
        panel_sign_in.setLayout(panel_sign_inLayout);
        panel_sign_inLayout.setHorizontalGroup(
            panel_sign_inLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_sign_inLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel_sign_inLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel_sign_inLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(48, 48, 48)
                        .addComponent(txt_server_ip, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
                    .addGroup(panel_sign_inLayout.createSequentialGroup()
                        .addGroup(panel_sign_inLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label_user)
                            .addComponent(label_password))
                        .addGap(31, 31, 31)
                        .addGroup(panel_sign_inLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(TBox_user_name, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_sign_inLayout.createSequentialGroup()
                        .addComponent(button_login)
                        .addGap(18, 18, 18)
                        .addComponent(btn_register)))
                .addContainerGap())
        );
        panel_sign_inLayout.setVerticalGroup(
            panel_sign_inLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_sign_inLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel_sign_inLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txt_server_ip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panel_sign_inLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TBox_user_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_user))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panel_sign_inLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_password))
                .addGap(18, 18, 18)
                .addGroup(panel_sign_inLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_register)
                    .addComponent(button_login))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout diolog_loginLayout = new javax.swing.GroupLayout(diolog_login.getContentPane());
        diolog_login.getContentPane().setLayout(diolog_loginLayout);
        diolog_loginLayout.setHorizontalGroup(
            diolog_loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diolog_loginLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel_sign_in, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        diolog_loginLayout.setVerticalGroup(
            diolog_loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diolog_loginLayout.createSequentialGroup()
                .addComponent(panel_sign_in, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        progressBar.setName("progressBar"); // NOI18N

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents
    AbstractAction updateplayers = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            try {
                serverComm.get_player_list(ip);
                serverComm.get_game_list(ip);
            } catch (IOException ex) {
                Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };



   public void process_message(Hashtable result) throws IOException{
        System.out.println("I am in clinetView: " + result );
        if ( (result.get("type").equals("command")) && (result.get("action").equals("ping")) ){
            System.out.println("I am inside if");
            try {
                serverComm.ping_reply(ip);
            } catch (IOException ex) {
                Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if ( (result.get("type").equals("reply")) && (result.get("status").equals("ok_login")) ){
            statusMessageLabel.setText("Connected");
            serverComm.get_player_list(ip);
            diolog_login.setVisible(false);
            player_game_list_panel.setVisible(true);
            update_player_timer = new Timer(3000, updateplayers);
            update_player_timer.start();

        }
        if ( (result.get("type").equals("reply")) && (result.get("status").equals("game_cancelled")) ){
            ;update_player_timer.start();
            player_game_list_panel.setVisible(true);
            team_select_panel.setVisible(false);

        }

        if ( (result.get("type").equals("reply")) && (result.get("status").equals("ok_create")) ){
            player_game_list_panel.setVisible(false);
            team_select_panel.setVisible(true);
            update_player_timer.stop();
            if(chat == null){
            chat = new IpChat_Main(this);
        }

        }
     if ( (result.get("type").equals("data")) && (result.get("data_type").equals("active_users")) ){

            String all_users = (String) result.get("active_users");

            String[] list = all_users.split(",");
            int j = 0;

            UI_list_team_1.removeAll();
            UI_list_team_2.removeAll();
            for(String i:list){
                String[] row = i.split(" ");
                Hashtable row_hash = new Hashtable();
                row_hash.put("username", row[0]);
                row_hash.put("ip", row[1]);
                row_hash.put("lvl", row[2]);
                row_hash.put("team", row[3]);
                if(!game_users.contains(row_hash)){
                    game_users.add(row_hash);
                }
                if(row_hash.get("team").equals("2")){
                    UI_list_team_2.add(row[0]+" ("+row[2]+")");
                }
                if(row_hash.get("team").equals("1")){
                    UI_list_team_1.add(row[0]+" ("+row[2]+")");
                }
               
            }
            if(chat == null){
            chat = new IpChat_Main(game_users, this);
            }else chat.update_HashTableArray(game_users);
            System.out.println(user_list.toString());

         }


         if ( (result.get("type").equals("data")) && (result.get("data_type").equals("online_user_list")) ){
            
            String all_users = (String) result.get("users");

            String[] list = all_users.split(",");
            int j = 0;

            UI_online_player.removeAll();
            for(String i:list){
                String[] row = i.split(" ");
                Hashtable row_hash = new Hashtable();
                row_hash.put("username", row[0]);
                row_hash.put("ip", row[1]);
                row_hash.put("lvl", row[2]);
                if(!user_list.contains(row_hash)){
                    user_list.add(row_hash);
                }

                UI_online_player.add(row[0]+" ("+row[2]+")");
            }
            System.out.println(user_list.toString());
            
         }


         if ( (result.get("type").equals("data")) && (result.get("data_type").equals("game_list")) ){

            String all_users = (String) result.get("game_list");
            if (all_users.equals("null")){
                UI_online_games.removeAll();
                return;
            }
            String[] list = all_users.split(",");
            int j = 0;

            UI_online_games.removeAll();
            for(String i:list){
                String[] row = i.split(" ");
                Hashtable row_hash = new Hashtable();
                row_hash.put("gameowner", row[0]);
                row_hash.put("ip", row[1]);
                row_hash.put("players", row[2]);
                if(!user_list.contains(row_hash)){
                    user_list.add(row_hash);
                }

                UI_online_games.add(row[0]+" ("+row[2]+")");
            }
            System.out.println(user_list.toString());

         }
        if((result.get("type").equals("gamedata")))
            thisview.processreceive(result);
        if(result.get("type").equals("gamestart"))
            populateandstart();
    
   }
    private void button_loginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button_loginMouseClicked
      //  ip = txt_server_ip.getText();
        _myUser = TBox_user_name.getText();
        statusMessageLabel.setText("Connecting to server....");
        System.out.println("Going to servercomm class for login");
        
        try {
            
            serverComm.login(TBox_user_name.getText(), jPasswordField1.getText(), ip);
            

        } catch (IOException ex) {
            Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
       //     statusMessageLabel.setText("Not Connected");
        }
        
        
    }//GEN-LAST:event_button_loginMouseClicked

    private void btn_registerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_registerMouseClicked
    //    if (diolog_register == null) {
            JFrame mainFrame = ClientApp.getApplication().getMainFrame();
         //   diolog_register = new ClientAboutBox(mainFrame);
            diolog_register.setLocationRelativeTo(mainFrame);
            diolog_register.setVisible(true);

      //  }
       
    }//GEN-LAST:event_btn_registerMouseClicked

    private void btn_reg_sendMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_reg_sendMouseClicked
        if(pwd_password.getText().equals(pwd_password2.getText())){
             System.out.println("Going to servercomm class for registration");
            try {
               
                serverComm.register(txt_new_user.getText(), pwd_password.getText(), ip);
            } catch (IOException ex) {
                Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else System.out.println("Passwords don't match......");

    }//GEN-LAST:event_btn_reg_sendMouseClicked

    private void menuItemLogoutMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menuItemLogoutMouseClicked
      //      diolog_register.setVisible(true);
    }//GEN-LAST:event_menuItemLogoutMouseClicked

    private void meunItemLoginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_meunItemLoginMouseClicked
    //    diolog_register.setVisible(true);
    }//GEN-LAST:event_meunItemLoginMouseClicked

    private void meunItemLoginMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_meunItemLoginMousePressed
        JFrame mainFrame = ClientApp.getApplication().getMainFrame();
        diolog_login.setLocationRelativeTo(mainFrame);
        diolog_login.setVisible(true);
    }//GEN-LAST:event_meunItemLoginMousePressed

    private void btn_createMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_createMouseClicked
        if(checkIfNumber(txt_on_player.getText())){
            btn_start.setVisible(true);
            int player_total = Integer.parseInt(txt_on_player.getText())*2;
            try {
                serverComm.create_game(_myUser, player_total, ip);
                _max_user = Integer.parseInt(txt_on_player.getText());
                is_game_owner = true;
                game_owner = _myUser;
            } catch (IOException ex) {
                Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_btn_createMouseClicked

    private void sendTMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sendTMouseClicked
        // TODO add your handling code here:
        IpChat_Main.sendT(chatM.getText());
        // System.out.flush();
}//GEN-LAST:event_sendTMouseClicked

    private void sendAMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sendAMouseClicked
        // TODO add your handling code here:
        IpChat_Main.sendA(chatM.getText());
        // System.out.flush();
}//GEN-LAST:event_sendAMouseClicked

    private void btn_joinMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_joinMouseClicked
        if(UI_online_games.getSelectedItem() == null){
            return;
        }
        player_game_list_panel.setVisible(false);
        team_select_panel.setVisible(true);
        
        is_game_owner = false;
        String[] join_game = UI_online_games.getSelectedItem().split(" ");
        try {
            _myTeam = 1;
            serverComm.join_game(join_game[0], _myUser, ip, 1);
            game_owner = join_game[0];
        } catch (IOException ex) {
            Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btn_joinMouseClicked

    private void btn_leaveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_leaveMouseClicked
        player_game_list_panel.setVisible(true);
        team_select_panel.setVisible(false);
        if(is_game_owner){
            try {
                serverComm.cancel_game(_myUser, ip);
            } catch (IOException ex) {
                Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }else{
            try {
                serverComm.leave_game(game_owner, _myUser, ip);
                update_player_timer.start();
            } catch (IOException ex) {
                Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }//GEN-LAST:event_btn_leaveMouseClicked

    private void btn_startMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_startMouseClicked
        
        populateandstart();

    }//GEN-LAST:event_btn_startMouseClicked

    private void btn_to_team2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_to_team2MouseClicked
        try {
            serverComm.leave_game(game_owner, _myUser, ip);
            _myTeam = 2;
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
            }
            serverComm.join_game(game_owner, _myUser, ip, _myTeam);
        } catch (IOException ex) {
            Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btn_to_team2MouseClicked

    private void btn_to_team_1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_to_team_1MouseClicked
        try {
            serverComm.leave_game(game_owner, _myUser, ip);
            _myTeam = 1;
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
            }
            serverComm.join_game(game_owner, _myUser, ip, _myTeam);
        } catch (IOException ex) {
            Logger.getLogger(ClientView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btn_to_team_1MouseClicked

    private void populateandstart()
    {
        update_player_timer.stop();
        //update_game_timer.stop();
        game.setVisible(true);
        player_game_list_panel.setVisible(false);
        team_select_panel.setVisible(false);
        int lvl = 0;
        int team = 0;
        for(int i=0; i< game_users.size();i++){
            String username = (String) game_users.get(i).get("username");
            lvl = Integer.parseInt((String) game_users.get(i).get("lvl"));
            team = Integer.parseInt((String) game_users.get(i).get("team"));
            String user_ip = (String) game_users.get(i).get("ip");
            if(username.equals(_myUser)){
                gameinfo = new GameInfo(_myUser, lvl, 0);
            }
            else if(team == _myTeam)
            {
                gameinfo.populatemyteam(username, lvl, user_ip);
            }else
                gameinfo.populateotherteam(username, lvl, user_ip);

        }
        //add this user information
         //not sure where is the level and experience points
        //add enemyteam
    //    gameinfo.populateotherteam("yoda", 1, "172.19.171.225");
        //add myown team
        //gameinfo.populatemyteam(ip, _max_user, ip);
        thisview = game.setinfo(serverComm,gameinfo);
        game.start();
    }
     public void setchatM(String test){
        chatM.setText(test);
    }

    public String getchatM(){
        String msg = chatM.getText();
        return msg;
    }


    public void setchatH(){
        StyledDocument doc = chatH.getStyledDocument();
        javax.swing.text.Style  Red = doc.addStyle("Red", null);
        StyleConstants.setForeground(Red, Color.red);

        javax.swing.text.Style Blue = doc.addStyle("Blue", null);
        StyleConstants.setForeground(Blue, Color.blue);
        StyleConstants.setFontSize(Red, 100);
    }




    public void addchatH(String msgH, String color){
        chatM.setText(null);

        StyledDocument doc = chatH.getStyledDocument();
        try {
            if(color == null ? "Blue" == null : color.equals("Blue"))
                doc.insertString(doc.getLength(),"\n" + msgH, doc.getStyle("Blue"));

            else
                doc.insertString(doc.getLength(),"\n" + msgH, doc.getStyle("Red"));

        } catch (BadLocationException e) {
         e.printStackTrace();
         }

    }


     public boolean checkIfNumber(String in) {

        try {

            Integer.parseInt(in);

        } catch (NumberFormatException ex) {
            return false;
        }

        return true;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu Account;
    private javax.swing.JTextField TBox_user_name;
    private java.awt.List UI_list_team_1;
    private java.awt.List UI_list_team_2;
    private java.awt.List UI_online_games;
    private java.awt.List UI_online_player;
    private javax.swing.JButton btn_create;
    private javax.swing.JButton btn_join;
    private javax.swing.JButton btn_leave;
    private javax.swing.JButton btn_reg_send;
    private javax.swing.JButton btn_register;
    private javax.swing.JButton btn_start;
    private javax.swing.JButton btn_to_team2;
    private javax.swing.JButton btn_to_team_1;
    private javax.swing.JButton button_login;
    private javax.swing.JTextPane chatH;
    private javax.swing.JTextArea chatM;
    private javax.swing.JPanel chatPanel;
    private javax.swing.JDialog diolog_login;
    private javax.swing.JDialog diolog_register;
    private typinggame.TypingGame game;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label_password;
    private javax.swing.JLabel label_user;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem menuItemLogout;
    private javax.swing.JMenuItem meunItemLogin;
    private javax.swing.JPanel panel_sign_in;
    private javax.swing.JPanel player_game_list_panel;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JPasswordField pwd_password;
    private javax.swing.JPasswordField pwd_password2;
    private javax.swing.JButton sendA;
    private javax.swing.JButton sendT;
    private javax.swing.JMenuItem serverProperties;
    private javax.swing.JMenu settingsMenu;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JPanel team_select_panel;
    private javax.swing.JTextField txt_new_user;
    private javax.swing.JTextField txt_on_player;
    private javax.swing.JTextField txt_server_ip;
    private javax.swing.JTextField txt_server_rgt;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
